package com.taxicalculator.dao;

import java.util.ArrayList;
import java.util.List;

import com.taxicalculator.dto.Review;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class ReviewSQLite extends SQLiteOpenHelper implements IReviewDAO {

	private static final String DATABASE_NAME = "CalculatorReview";
	private static final int DATABASE_VERSION = 0;
	private static final String REVIEW_TABLE = "review";
	private static final String REVIEW_ID="review_id" ;
	private static final String CALCULATOR_REVIEW = "calculator_review";
	private static final String CALCULATOR_RATING = "calculator_rating";

	public ReviewSQLite(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}
	
	public void onCreate(SQLiteDatabase db)
	{
		String schema= "CREATE TABLE" + REVIEW_TABLE + "(" + REVIEW_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," +CALCULATOR_REVIEW+" TEXT NOT NULL, "+CALCULATOR_RATING+" TEXT NOT NULL);";
		schema.length();
		db.execSQL(schema);
	}
	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}
	 public void save(Review review) throws Exception {
		}
	    
	    /**
	     * List all reviews.
	     * @param List all reviews in the database.
	     * @throws Exception any problem in the underlying persistence mechanism.
	     */
	    public List<Review> getReviews() throws Exception {
	    	
	    	 // Create the return hash
            List<Review> allReviews = new ArrayList<Review>();

            // SQL statement
            String selectSQL = "SELECT * FROM " + REVIEW_TABLE ;


            // Create the result set
            Cursor cursor = getReadableDatabase().rawQuery(selectSQL, null);

            // Iterate over the results
            if (cursor.getCount() > 0) {
                    

                    // Move the cursor to the very first result.
                    cursor.moveToFirst();

                    // Iterate over our results.  Keep iterating as long as we're not after the last row.
                    while(!cursor.isAfterLast()) {

                            // Define user object
                            Review review = new Review();

                            // Fill the user table with current user
                            review.setReviewId(cursor.getInt(0));
                            review.setCalcRating(cursor.getInt(1));
                            review.setCalcReview(cursor.getString(2));
                            allReviews.add(review);

                          

                            // Continue the loop
                            cursor.moveToNext();

                    }
                    // Close the process at the end
                    cursor.close();

            } 

            return allReviews;
    
		}

}
